package com.dhcc.cdc.sys.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.dao.ConfigurationMapper;
import com.dhcc.cdc.sys.entity.CdcProductEntity;
import com.dhcc.cdc.sys.entity.ConfigurationEntity;
import com.dhcc.cdc.sys.service.ICdcProductService;
import com.dhcc.cdc.sys.service.IConfigurationService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 配置信息表数据服务层接口
 * 
 * @ClassName: ConfigurationServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-04-16 17:11:56
 */
@Service("configurationService")
public class ConfigurationServiceImpl extends BaseServiceImpl<ConfigurationMapper, ConfigurationEntity> implements IConfigurationService {
    @Autowired
    private ICdcProductService cdcProductService;
    @Autowired
    ICdcProductService productSrv;

	@Override
	public List<ConfigurationEntity> page(Page<ConfigurationEntity> page, Map<String, Object> map) {
        String productCode=(String) map.get("productCode");
        if(productCode != null){
            String productId = String.valueOf(getProductId(productCode));
            if(productId != null) map.put("productId", productId);
        }
        List<ConfigurationEntity> result=this.baseMapper.page(page, map);
		return result;
	}
	@Override
	public List<ConfigurationEntity> list(Map<String, Object> map) {
        String productCode=(String) map.get("productCode");
        if(productCode != null){
            String productId = String.valueOf(getProductId(productCode));
            if(productId != null) map.put("productId", productId);
        }
		List<ConfigurationEntity> result=this.baseMapper.list(map);
		return result;
	}

    @Override
    public ConfigurationEntity selectByProductCodeAndCode(String productCode, String code) {
        ConfigurationEntity configurationEntity = null;
        CdcProductEntity cdcProductEntity = cdcProductService.selectByCode(productCode);
        if (cdcProductEntity != null) {
             configurationEntity = this.baseMapper.selectByProductCodeAndCode(cdcProductEntity.getId(), code);
        }
        return configurationEntity;
    }

    @Override
    public ConfigurationEntity selectByProductCodeAndCode(String productCode, String code, Long hospitalId) {
        ConfigurationEntity configurationEntity = null;
        CdcProductEntity cdcProductEntity = cdcProductService.selectByCode(productCode);
        if (cdcProductEntity != null) {
            configurationEntity = this.baseMapper.selectByProductCodeAndCodeAndHospId(cdcProductEntity.getId(), code, hospitalId);
            if (configurationEntity == null) {
                configurationEntity = this.baseMapper.selectByProductCodeAndCode(cdcProductEntity.getId(), code);
            }
        }
        return configurationEntity;
    }

    public List<ConfigurationEntity> filtByProCode(List<ConfigurationEntity> list, String productCode){
        if(productCode != null){
            list = list.stream()
                .filter((ConfigurationEntity e) -> e.getProduct().getCode().contains(productCode))
                .collect(Collectors.toList());
        }
        return list;
    }

    public Long getProductId(String productCode) {
        Long productId=null;
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code", productCode);
        CdcProductEntity product = this.productSrv.list(map).get(0);
        if(product != null){
            productId=product.getId();
        }

        return productId;
    }

}